package com.gy.algorithm.carl.hashtableexa;

import java.util.*;

import static org.apache.tomcat.jni.Poll.add;

/**
 * @BelongsProject: javabase
 * @BelongsPackage: com.gy.algorithm.carl
 * @Author: gy
 * @Date: 2024/10/22 11:30
 * @Description:
 */
public class ThreeSum15 {

    public static void main(String[] args) {
        int[] test = new int[] {-1,0,1,2,-1,-4};
        List<List<Integer>> lists = threeSum(test);
        System.out.println(lists);
    }

    // end1
//    public static List<List<Integer>> threeSum(int[] nums) {
//
//    }

    // write
    public static List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> temps = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            for (int j = i+1 ; j < nums.length; j++) {
                for (int k = j+1; k < nums.length; k++) {
                    if(nums[i] + nums[j] + nums[k] == 0) {
                        temps.clear();
                        temps.add(nums[i]);
                        temps.add(nums[j]);
                        temps.add(nums[k]);
                        if(!contailAll(temps,res)){
                            ArrayList<Integer> integers = new ArrayList<>();
                            integers.add(nums[i]);
                            integers.add(nums[j]);
                            integers.add(nums[k]);
                            res.add(integers);
                            break;
                        }
                    }
                }
            }
        }
        return res;
    }

    private static boolean contailAll(List<Integer> temp,List<List<Integer>> all){
        for (List<Integer> integers : all) {
            boolean res = false;
            for (Integer i : temp) {
                if(new HashSet<>(integers).equals(new HashSet<>(temp))){
                    return true;
                }
            }
        }
        return false;
    }
}
